package base

import (
	"fmt"
	"io"
	"os"
	"time"
)

func LogWrite(log string, filename string) {
	var err error
	var out *os.File
	defer out.Close()
	filepath := fmt.Sprintf("%v/%v.log", Config.LogPath, filename)
	if checkFile(filepath) {
		out, err = os.OpenFile(filepath, os.O_APPEND|os.O_WRONLY, 0666) //打开文件,第二个参数是写入方式和权限
		if err != nil {
			fmt.Println("文件存在，已打开")
		}
	} else {
		out, err = os.Create(filepath)
		if err != nil {
			fmt.Println("创建文件失败")
		}
	}

	_, err = io.WriteString(out, time.Now().Format("2006-01-02 15:04:05")+"："+log+"\n")
	if err != nil {
		fmt.Println(err)
	}
}

func checkFile(filepath string) bool {
	var exist = true
	if _, err := os.Stat(filepath); os.IsNotExist(err) {
		exist = false
	}
	return exist
}
